*Code below for Figures 5.1 5.2 5.6 
*Code below for Tables 5.3 5.5 A.12 A.13 A.14

clear
clear matrix
clear mata
set more off
set maxvar 9000
use "C:\Users\Andy baker\OneDrive - UCB-O365\Stata\Barry\Final\OmnibusBoth.dta", clear

fillin id wave
xtset id wave
recode respond .=0
drop if suspect==1

recode bairroibge  129=. 200=. 202=. 205=. 211=. 213=. 256=. 287=. 307=.
recode bairroamob  129=. 200=. 202=. 205=. 211=. 213=. 256=. 287=. 307=.

drop if bairroibge==.
gen temp=bairroibge if wave==1
egen bairroibgecheck=max(temp), by(id)
replace bairroibge=bairroibgecheck
label values bairroibge bairro
drop temp

merge m:1 bairroibge using "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 5 Neighborhoods\Data\mergeTwoCity.dta"
drop _merge
xtset id wave

*DESCRIPTIVE STUFF
*City Polarization
	*feeling therms of parties and groups

mvdecode v42a_pidthermpsdb v42b_pidthermpfl v42c_pidthermpt v42d_pidthermpmdb v42e_pidthermpsb v42f_pidthermpps v40b_thermCUT v40i_thermMST v40a_thermmilitary  v40c_thermpublicsectorworkers  v40d_thermbusiness  v40e_thermnortheasterners  v40f_thermgays v40g_thermevangelicals v40h_thermafrobrazilians, mv(18/19)
mvdecode v42a_pidthermpsdb v42b_pidthermpfl v42c_pidthermpt v42d_pidthermpmdb v42e_pidthermpsb v42f_pidthermpps v40b_thermCUT v40i_thermMST v40a_thermmilitary  v40c_thermpublicsectorworkers  v40d_thermbusiness  v40e_thermnortheasterners  v40f_thermgays v40g_thermevangelicals v40h_thermafrobrazilians, mv(418)

egen party_ft_mean=rowmean(v42a_pidthermpsdb v42b_pidthermpfl v42c_pidthermpt v42d_pidthermpmdb v42e_pidthermpsb v42f_pidthermpps) if wave==1
egen groups_ft_mean=rowmean(v40b_thermCUT v40i_thermMST v40a_thermmilitary  v40c_thermpublicsectorworkers  v40d_thermbusiness  v40e_thermnortheasterners  v40f_thermgays v40g_thermevangelicals v40h_thermafrobrazilians) if wave==1

egen v42d_pidthermpmdb_std =std(v42d_pidthermpmdb -party_ft_mean)
egen v42c_pidthermpt_std = std(v42c_pidthermpt -party_ft_mean)
egen v40b_thermCUT_std =std(v40b_thermCUT -groups_ft_mean)
egen v40i_thermMST_std=std(v40i_thermMST -groups_ft_mean)
egen v40a_thermmilitary_std=std(v40a_thermmilitary  -groups_ft_mean)

*FIGURE 5.2
ci means v42d_pidthermpmdb_std if v43b_pidparty==4 & wave==1 & city==1
ci means v42d_pidthermpmdb_std if v43b_pidparty==4 & wave==1 & city==2

ci means v42c_pidthermpt_std if v43b_pidparty==1 & wave==1 & city==1
ci means v42c_pidthermpt_std if v43b_pidparty==1 & wave==1 & city==2

ci means v40b_thermCUT_std if v43b_pidparty==1 & wave==1 & city==1
ci means v40b_thermCUT_std if v43b_pidparty==1 & wave==1 & city==2

ci means v40i_thermMST_std if v43b_pidparty==1 & wave==1 & city==1
ci means v40i_thermMST_std if v43b_pidparty==1 & wave==1 & city==2

recode v22h2_presreject2lula v22h4_presreject4serr (.=0)
	
ci means v22h2_presreject2lula if l.v43b_pidparty==1 & wave==2 & city==1 & respond==1
ci means v22h2_presreject2lula if l.v43b_pidparty==1 & wave==2 & city==2 & respond==1

ci means v22h4_presreject4serr if l.v43b_pidparty==4 & wave==2 & city==1 & respond==1
ci means v22h4_presreject4serr if l.v43b_pidparty==4 & wave==2 & city==2 & respond==1

corr v42d_pidthermpmdb_std  v42c_pidthermpt_std if city==1
corr v42d_pidthermpmdb_std  v42c_pidthermpt_std if city==2

*straight ticket voting
gen vote98pres_split=v51b_vote98pres if v51b_vote98pres <6
label values vote98pres_split pres98z
gen vote98CXgov_split=v52bcs_vote98gov if v52bcs_vote98gov <4
label values vote98CXgov_split csgov98z
gen vote98JFgov_split=v52bjf_vote98gov if v52bjf_vote98gov<4 
label values vote98JFgov_split jfgov98z
gen vote98CXmayor_split = v54bcs_vote00mayor if v54bcs_vote00mayor<4
label values vote98CXmayor_split csmayor98z
gen vote98JFmayor_split = v54bjf_vote00mayor if v54bjf_vote00mayor <5
label values vote98JFmayor_split mayor98z

replace vote98pres_split = . if wave~=1
replace vote98CXgov_split = . if wave~=1
replace vote98JFgov_split = . if wave~=1
replace vote98CXmayor_split = . if wave~=1
replace vote98JFmayor_split = . if wave~=1
replace vote98pres_split = . if s3a_timeincity<3
replace vote98CXgov_split = . if s3a_timeincity<3
replace vote98JFgov_split = . if s3a_timeincity<3
replace vote98CXmayor_split = . if s3a_timeincity<3
replace vote98JFmayor_split = . if s3a_timeincity<3

tab vote98pres_split vote98CXgov_split, row
tab vote98pres_split vote98JFgov_split , row
tab vote98pres_split vote98CXmayor_split , row
tab vote98pres_split vote98JFmayor_split , row


tab v23ccs_govvote  v22c_presvote if wave==3 & v22c_presvote<17 & v23ccs_govvote<17 , col
*Table 5.3

tab v43b_pidparty if city==2 & wave==1 & respond==1 & v43b_pidparty~=28 & v43b_pidparty~=29 & v43a_pidyesorno~=. & v43a_pidyesorno<3, m
tab v43b_pidparty if city==2 & wave==3 & respond==1 & v43b_pidparty~=28 & v43b_pidparty~=29 & v43a_pidyesorno~=. & v43a_pidyesorno<3, m
tab v43b_pidparty if city==1 & wave==1 & respond==1 & v43b_pidparty~=28 & v43b_pidparty~=29 & v43a_pidyesorno~=. & v43a_pidyesorno<3, m
tab v43b_pidparty if city==1 & wave==3 & respond==1 & v43b_pidparty~=28 & v43b_pidparty~=29 & v43a_pidyesorno~=. & v43a_pidyesorno<3, m


*Participation in groups and party contacts
mvdecode v14a_groupsamob v14b_groupssports v14c_groupschurch v14d_groupsunion v14e_groupsparty v15c_meetselfhelp v15d_meetculture v15e_meetsocial v15a_meetfriends v38a_persuadefrompartyyesorno, mv(8/9)
tab1 v14a_groupsamob v14b_groupssports v14c_groupschurch v14d_groupsunion v14e_groupsparty  v15c_meetselfhelp v15d_meetculture v15e_meetsocial, nolab
gen f2v14a_groupsamob =f2.v14a_groupsamob 
gen f2v14b_groupssports =f2.v14b_groupssports 
gen f2v14d_groupsunion =f2.v14d_groupsunion 
gen f2v14e_groupsparty  =f2.v14e_groupsparty  
egen groups=anycount(f2v14a_groupsamob f2v14b_groupssports f2v14d_groupsunion f2v14e_groupsparty v15c_meetselfhelp  v15d_meetculture v15e_meetsocial), values(3 4)
recode groups 0=. if wave~=3 | respond~=1
tab groups if wave==3 & respond==1
tab v15a_meetfriends if wave==3 & respond==1
tab v38a_persuadefrompartyyesorno if wave==3 & respond==1 

*residential mobility
tab s3a_timeincity  if city==2 & wave==1
tab s3a_timeincity  if city==1 & wave==1
tab s3b_timeinbairro if city==1 & wave==1
tab s3b_timeinbairro if city==2 & wave==1
*used these to calculate pie chart
tab v17_conversewhommostfreq if city==1 & wave==1 & v17_conversewhommostfreq <8
tab v17_conversewhommostfreq if city==2 & wave==1 & v17_conversewhommostfreq <8
tab v18_friendssource if city==1 & wave==1 & v18_friendssource<8
tab v18_friendssource if city==2 & wave==1 & v18_friendssource<8

recode v16e_conversefriends (8=.) (9=.) (1=4) (2=3) (3=2) (4=1) 
replace v16e_conversefriends =. if wave~=1
replace v16b_conversenotamob =. if wave~=1
egen converse1=rowmean(v16e_conversefriends v16b_conversenotamob)

tab v74b_disc1relate city if v74b_disc1relate<7, col
tab v75b_disc2relate city if v75b_disc2relate<7, col
tab v76b_disc3relate city if v76b_disc3relate<7, col

*used to calculate pie charts--doing it a clunky way
recode v74b_disc1relate v75b_disc2relate v76b_disc3relate (1=1) (2/6=0) (7/9=.) (else=.) if wave==2, gen(spouse1 spouse2 spouse3)
recode v74b_disc1relate v75b_disc2relate v76b_disc3relate (2=1) (1=0) (3/6=0) (7/9=.) (else=.)  if wave==2, gen(relative1 relative2 relative3)
recode v74b_disc1relate v75b_disc2relate v76b_disc3relate (3=1) (1/2=0) (4/6=0) (7/9=.) (else=.) if wave==2, gen(child1 child2 child3)
recode v74b_disc1relate v75b_disc2relate v76b_disc3relate (4=1) (1/3=0) (5/6=0) (7/9=.) (else=.) if wave==2, gen(neigh1 neigh2 neigh3)
recode v74b_disc1relate v75b_disc2relate v76b_disc3relate (5=1) (1/4=0) (6=0) (7/9=.) (else=.) if wave==2, gen(work1 work2 work3)
recode v74b_disc1relate v75b_disc2relate v76b_disc3relate (6=1) (1/5=0) (7/9=.) (else=.) if wave==2, gen(other1 other2 other3)

egen spouse=rowtotal(spouse1 spouse2 spouse3)
egen relative=rowtotal(relative1 relative2 relative3)
egen child=rowtotal(child1 child2 child3)
egen neigh=rowtotal(neigh1 neigh2 neigh3)
egen work=rowtotal(work1 work2 work3)
egen other=rowtotal(other1 other2 other3)

egen disctotal_relations=rowtotal(spouse1 spouse2 spouse3 relative1 relative2 relative3 child1 child2 child3 neigh1 neigh2 neigh3 work1 work2 work3 other1 other2 other3)
tab disctotal if wave==2 & respond==1 & whennew~=2
display 427+533*2+1960*3

tab disctotal city if wave==2 & respond==1 & whennew~=2
display 203+257*2+944*3
display 224+276*2+1016*3

tab spouse city, col
display 867/7373
display 402/3549
display 464/3824

tab relative city, col
display (874+512*2+150*3)/7373
display (428+246*2+65*3)/3549
display (446+266*2+85*3)/3824

tab child city, col
display (393+147*2+24*3)/7373
display (204+78*2+12*3)/3549
display (189+69*2+12*3)/3824

tab neigh city, col
display (619+249*2+122*3)/7373
display (326+141*2+69*3)/3549
display (293+108*2+53*3)/3824

tab work city, col
display (519+255*2+157*3)/7373
display (233+104*2+63*3)/3549
display (286+151*2+94*3)/3824

tab other city, col
display (214+57*2+33*3)/7373
display (94+29*2+17*3)/3549
display (120+28*2+16*3)/3824

xtset id wave

gen disc1_bairro=f.d1_bairro if wave==2
gen disc2_bairro=f.d2_bairro if wave==2
gen disc1_order=f.d1_orderinmainrespondentlist if wave==2
gen disc2_order=f.d2_orderinmainrespondentlist if wave==2
label values disc1_bairro bairroz

*need to look into this given ambiguities around cohab
recode disc1_bairro (151/152=101) (156/157=107) (166=116) (168=118) (174/175=123) (177=127) 
recode disc2_bairro (151/152=101) (156/157=107) (166=116) (168=118) (174/175=123) (177=127) 

	*relatives
gen samebairro1=0 if (v74b_disc1relate==2 & disc1_order==1 & disc1_bairro~=.) | (v74b_disc1relate==2 & disc2_order==1 & disc2_bairro~=.)
replace samebairro1=1 if bairroibge==disc1_bairro & samebairro1==0
replace samebairro1=1 if bairroibge==disc2_bairro & samebairro1==0

gen samebairro2=0 if (v75b_disc2relate==2 & disc1_order==2 & disc1_bairro~=.) | (v75b_disc2relate==2 & disc2_order==2 & disc2_bairro~=.)
replace samebairro2=1 if bairroibge==disc1_bairro & samebairro2==0
replace samebairro2=1 if bairroibge==disc2_bairro & samebairro2==0

gen samebairro3=0 if (v76b_disc3relate==2 & disc1_order==3 & disc1_bairro~=.) | (v76b_disc3relate==2 & disc2_order==3 & disc2_bairro~=.)
replace samebairro3=1 if bairroibge==disc1_bairro & samebairro3==0
replace samebairro3=1 if bairroibge==disc2_bairro & samebairro3==0

tab1 samebairro1 - samebairro3
tab samebairro1 city, col
tab samebairro2 city, col
tab samebairro3 city, col
*percent of relatives in same bairro
display (255+138+71)/(422+246+131)
drop samebairro1 - samebairro3

	*child
gen samebairro1=0 if (v74b_disc1relate==3 & disc1_order==1 & disc1_bairro~=.) | (v74b_disc1relate==3 & disc2_order==1 & disc2_bairro~=.)
replace samebairro1=1 if bairroibge==disc1_bairro & samebairro1==0
replace samebairro1=1 if bairroibge==disc2_bairro & samebairro1==0

gen samebairro2=0 if (v75b_disc2relate==3 & disc1_order==2 & disc1_bairro~=.) | (v75b_disc2relate==3 & disc2_order==2 & disc2_bairro~=.)
replace samebairro2=1 if bairroibge==disc1_bairro & samebairro2==0
replace samebairro2=1 if bairroibge==disc2_bairro & samebairro2==0

gen samebairro3=0 if (v76b_disc3relate==3 & disc1_order==3 & disc1_bairro~=.) | (v76b_disc3relate==3 & disc2_order==3 & disc2_bairro~=.)
replace samebairro3=1 if bairroibge==disc1_bairro & samebairro3==0
replace samebairro3=1 if bairroibge==disc2_bairro & samebairro3==0

tab1 samebairro1 - samebairro3
tab samebairro1 city, col
tab samebairro2 city, col
tab samebairro3 city, col
*percent of children in same bairro
display (96+69+14)/(155+88+28)
drop samebairro1 - samebairro3

	*work/school
gen samebairro1=0 if (v74b_disc1relate==5 & disc1_order==1 & disc1_bairro~=.) | (v74b_disc1relate==5 & disc2_order==1 & disc2_bairro~=.)
replace samebairro1=1 if bairroibge==disc1_bairro & samebairro1==0
replace samebairro1=1 if bairroibge==disc2_bairro & samebairro1==0

gen samebairro2=0 if (v75b_disc2relate==5 & disc1_order==2 & disc1_bairro~=.) | (v75b_disc2relate==5 & disc2_order==2 & disc2_bairro~=.)
replace samebairro2=1 if bairroibge==disc1_bairro & samebairro2==0
replace samebairro2=1 if bairroibge==disc2_bairro & samebairro2==0

gen samebairro3=0 if (v76b_disc3relate==5 & disc1_order==3 & disc1_bairro~=.) | (v76b_disc3relate==5 & disc2_order==3 & disc2_bairro~=.)
replace samebairro3=1 if bairroibge==disc1_bairro & samebairro3==0
replace samebairro3=1 if bairroibge==disc2_bairro & samebairro3==0

tab1 samebairro1 - samebairro3
tab samebairro1 city, col
tab samebairro2 city, col
tab samebairro3 city, col
*percent of work/school in same bairro
display (51+48+29)/(201+154+112)
drop samebairro1 - samebairro3

	*other
gen samebairro1=0 if (v74b_disc1relate==6 & disc1_order==1 & disc1_bairro~=.) | (v74b_disc1relate==6 & disc2_order==1 & disc2_bairro~=.)
replace samebairro1=1 if bairroibge==disc1_bairro & samebairro1==0
replace samebairro1=1 if bairroibge==disc2_bairro & samebairro1==0

gen samebairro2=0 if (v75b_disc2relate==6 & disc1_order==2 & disc1_bairro~=.) | (v75b_disc2relate==6 & disc2_order==2 & disc2_bairro~=.)
replace samebairro2=1 if bairroibge==disc1_bairro & samebairro2==0
replace samebairro2=1 if bairroibge==disc2_bairro & samebairro2==0

gen samebairro3=0 if (v76b_disc3relate==6 & disc1_order==3 & disc1_bairro~=.) | (v76b_disc3relate==6 & disc2_order==3 & disc2_bairro~=.)
replace samebairro3=1 if bairroibge==disc1_bairro & samebairro3==0
replace samebairro3=1 if bairroibge==disc2_bairro & samebairro3==0

tab1 samebairro1 - samebairro3
tab samebairro1 city, col
tab samebairro2 city, col
tab samebairro3 city, col
*percent of other in same bairro
display (12+15+10)/(60+51+32)
drop samebairro1 - samebairro3

*Persuasion from AMOB
mvdecode v16i_converseamob  v37a_persuadefromamobyesorno, mv(8)
mvdecode v16i_converseamob  v37a_persuadefromamobyesorno, mv(9)
mvdecode v16i_converseamob  v37a_persuadefromamobyesorno, mv(99)

tab v16i_converseamob city if wave==3 | wave==2, col
tab v37a_persuadefromamobyesorno city if wave==3 | wave==2, col
tab v73b_amobpresvote if wave==3 & respond==1, m
tab v73dcs_amobfeddepvote if wave==3 & respond==1 & city==1, m
tab v73djf_amobfeddepvote if wave==3 & respond==1 & city==2, m
tab v73ecs_amobstdepvote if wave==3 & respond==1 & city==1, m
tab v73ejf_amobstdepvote if wave==3 & respond==1 & city==2, m
drop other1 other2 other3

xtset id wave
	
capture program drop getaggs
program define getaggs

while "`1'"~="" {
egen `1'sum=sum(`1'), by(bairroibge wave)
gen `1'smnon=`1'sum-`1'
gen n`1'=0
replace n`1'=1 if `1'~=.
egen sumn`1'=sum(n`1'), by(bairroibge wave)
gen `1'bairronomr=`1'smnon/(sumn`1'-1)
replace `1'bairronomr=. if bairroibge==.
drop `1'sum n`1' sumn`1' `1'smnon 
mac shift
}
end


capture program drop getaggs1
program define getaggs1

while "`1'"~="" {
egen `1'smnon1=sum(`1'), by(bairroibge wave)
gen n`1'1=0
replace n`1'1=1 if `1'~=.
egen sumn`1'1=sum(n`1'1), by(bairroibge wave)
gen `1'bairro=`1'smnon1/(sumn`1'1)
replace `1'bairro=. if bairroibge==.
drop n`1'1 sumn`1'1 `1'smnon1 
mac shift
}
end
	
	
*remember the Itamar decision here, and also probably the Roseana decision. THEY ARE IN THE DENOMINATOR

recode v22c_presvote 1=1 17/19=.  .=. else=0, gen(presvote_ciro)
recode v22c_presvote 2=1 17/19=. .=. else=0, gen(presvote_lula)
recode v22c_presvote 4=1 17/19=. .=. else=0, gen(presvote_serra)
recode v22c_presvote 5=1 17/19=. .=. else=0, gen(presvote_ag)

recode vote98pres 1=1 2=0 3=0 6/9=. .=. else=., gen(presvote_fhc98)
replace presvote_fhc98=. if s3b_timeinbairro<3
replace presvote_fhc98=. if wave~=1
recode vote98pres 1=0 2=1 3=0 6/9=. .=. else=., gen(presvote_lula98)
replace presvote_lula98=. if s3b_timeinbairro<3
replace presvote_lula98=. if wave~=1
recode vote98pres 1=0 2=0 3=1 6/9=. .=. else=., gen(presvote_ciro98)
replace presvote_ciro98=. if s3b_timeinbairro<3
replace presvote_ciro98=. if wave~=1

getaggs presvote_ciro presvote_lula presvote_serra presvote_ag 
getaggs1 presvote_ciro presvote_lula presvote_serra presvote_ag
getaggs presvote_fhc98 presvote_lula98 presvote_ciro98
getaggs1 presvote_fhc98 presvote_lula98 presvote_ciro98 

replace presvote_cirobairronomr =presvote_cirobairro if presvote_cirobairronomr ==. 
replace presvote_lulabairronomr =presvote_lulabairro if presvote_lulabairronomr ==. 
replace presvote_agbairronomr =presvote_agbairro if presvote_agbairronomr ==. 
replace presvote_serrabairronomr =presvote_serrabairro if presvote_serrabairronomr ==.

replace presvote_fhc98bairronomr =presvote_fhc98bairro if presvote_fhc98bairronomr ==. 
replace presvote_lula98bairronomr =presvote_lula98bairro if presvote_lula98bairronomr ==. 
replace presvote_ciro98bairronomr =presvote_ciro98bairro if presvote_ciro98bairronomr ==. 

*SES
recode s12_incomefamily 0/9=. 10000/55000=10000 20/90=100
recode s6_education 18/19=.

gen income=l2.s12_incomefamily if whennew==1
gen educ=l2.s6_education if whennew==1
replace income=l.s12_incomefamily if whennew==2
replace educ=l1.s6_education if whennew==2
replace income=s12_incomefamily if whennew==3
replace educ=s6_education if whennew==3

gen lnincome=ln(income)
factor lnincome educ if wave==3, pcf factors(1)
predict ses1 if wave==3  
impute ses1 lnincome  s6_education if wave==3, gen(ses)
recode v22c_presvote 2=0 4=1 else=., gen(serra_lula)
replace serra_lula=. if wave~=3
polychoric serra_lula ses if city==1
polychoric serra_lula ses if city==2
polychoric serra_lula lnincome if city==1
polychoric serra_lula lnincome if city==2

gen lnincome_iv1=ln(s12_incomefamily)
factor lnincome_iv1 s6_education if wave==1 & respond==1, pcf factors(1)
predict ses_temp if wave==1
impute ses_temp lnincome s6_education if wave==1 & respond==1, gen(ses_iv)

*bysort bairroibge: pwcorr serra_lula ses if city==1
*bysort city: summ cohesion

*PARTISAN INTENSITY
replace v44b_pidparty=29 if v43a_pidyesorno==1
recode v43b_pidparty (4=1) (else=0), gen(partyintensePT)
replace partyintensePT=.5 if v44b_pidparty==4
replace partyintensePT=. if respond==0

recode v43b_pidparty (3=1) (else=0), gen(partyintensePSDB)
replace partyintensePSDB=.5 if v44b_pidparty==3
replace partyintensePSDB=. if respond==0

recode v43b_pidparty (1=1) (else=0), gen(partyintensePMDB)
replace partyintensePMDB=.5 if v44b_pidparty==1
replace partyintensePMDB=. if respond==0

/*
recode v43b_pidparty (14=1) (else=0), gen(partyintensePSB)
replace partyintensePSB=.5 if v44b_pidparty==14
replace partyintensePSB=. if respond==0
*/

gen partyintense_baseline=0
replace partyintense_baseline=1 if partyintensePMDB==0 & partyintensePT==0 & partyintensePSDB==0
replace partyintense_baseline=.5 if v44b_pidparty==2 
replace partyintense_baseline=.5 if v44b_pidparty>4 & v44b_pidparty<20
replace partyintense_baseline=0 if v44b_pidparty==1
replace partyintense_baseline=0 if v44b_pidparty==3
replace partyintense_baseline=0 if v44b_pidparty==4
replace partyintense_baseline=. if wave~=1
replace partyintense_baseline=. if respond~=1

/*
recode v43b_pidparty (13=1) (else=0), gen(partyintensePPS)
replace partyintensePPS=.5 if v44b_pidparty==13
replace partyintensePPS=. if respond==0

recode v43b_pidparty (22=1) (else=0), gen(partyintensePSOL)
replace partyintensePSOL=.5 if v44b_pidparty==22
replace partyintensePSOL=. if respond==0
*/

*Evangelicals
recode s8b_religionfreqattend 4=0 3=1 2=2 1=3 8=0 9=0, gen(attend)
gen evangelical_attend=0 if respond==1
replace evangelical_attend=attend if s8a_religionaffiliation==2 
 
*Party contact
recode 	v38b1b_persuade1frompartyname v38b2b_persuade2frompartyname v38b3b_persuade3frompartyname (4=2) (12=2) (8=2) (9=2) (3=4) (1=4) (2=4) (5=4) (14=5) (13=1) (6=1) (7=1) (10=16) (11=16) (20=16) (21=16) (else=0), gen(partycontact1 partycontact2 partycontact3)
egen lulacontact=anycount(partycontact1 partycontact2 partycontact3), values(2)
egen serracontact=anycount(partycontact1 partycontact2 partycontact3), values(4)
egen cirocontact=anycount(partycontact1 partycontact2 partycontact3), values(1)
egen agcontact=anycount(partycontact1 partycontact2 partycontact3), values(5)
gen contactbaseline=0
replace contactbaseline=1 if lulacontact==0 & serracontact==0 & cirocontact==0 & agcontact==0
replace lulacontact=. if respond~=1 | wave~=3
replace serracontact=. if respond~=1  | wave~=3
replace cirocontact=. if respond~=1  | wave~=3
replace agcontact=. if respond~=1  | wave~=3

/*CONVERSATION FREQUENCY
polychoricpca v16b_conversenotamob v16i_converseamob v16k_converseelectionsamob, score(conversetemp) nscore(1) 
impute conversetemp v16b_conversenotamob v16i_converseamob v16k_converseelectionsamob  if respond==1 & wave==3, gen(converse)
*/

*AWARENESS
sort id
merge m:1 id using "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\awarez.dta"
drop _m

*DISCUSSANTS
recode v74c_disc1presvote v75c_disc2presvote v76c_disc3presvote (1=1) (17/19=.) (.=.) (else=0), gen(ciro1 ciro2 ciro3)
recode v74c_disc1presvote v75c_disc2presvote v76c_disc3presvote (2=1) (17/19=.) (.=.) (else=0), gen(lula1 lula2 lula3)
recode v74c_disc1presvote v75c_disc2presvote v76c_disc3presvote (4=1) (17/19=.) (.=.) (else=0), gen(serra1 serra2 serra3)
recode v74c_disc1presvote v75c_disc2presvote v76c_disc3presvote (5=1) (17/19=.) (.=.) (else=0), gen(ag1 ag2 ag3)
recode v74c_disc1presvote v75c_disc2presvote v76c_disc3presvote (6=1) (17/19=.) (.=.) (else=0), gen(itamar1 itamar2 itamar3)
recode v74c_disc1presvote v75c_disc2presvote v76c_disc3presvote (7=1) (17/19=.) (.=.) (else=0), gen(zemaria1 zemaria2 zemaria3)
recode v74c_disc1presvote v75c_disc2presvote v76c_disc3presvote (8=1) (17/19=.) (.=.) (else=0), gen(rcp1 rcp2 rcp3)
recode v74c_disc1presvote v75c_disc2presvote v76c_disc3presvote (16=1) (17/19=.) (.=.) (else=0), gen(other1 other2 other3)
egen totopinionateddisc=rsum(ciro1 ciro2 ciro3 lula1 lula2 lula3 serra1 serra2 serra3 ag1 ag2 ag3)
replace totopinionateddisc=. if respond~=1 | (whennew==2 & wave==2) | (whennew==2 & wave==3) | (wave==1) | (wave==4) | (whennew==3)
egen cirosum_numerator=rsum(ciro1 ciro2 ciro3)
egen lulasum_numerator=rsum(lula1 lula2 lula3)
egen serrasum_numerator=rsum(serra1 serra2 serra3)
egen agsum_numerator=rsum(ag1 ag2 ag3)
*egen othersum_numerator=rsum(itamar1 itamar2 itamar3 zemaria1 zemaria2 zemaria3 rcp1 rcp2 rcp3 other1 other2 other3)
gen cirodiscprop=cirosum_numerator / totopinionateddisc
gen luladiscprop= lulasum_numerator /totopinionateddisc
gen serradiscprop=serrasum_numerator /totopinionateddisc
gen agdiscprop= agsum_numerator/totopinionateddisc
recode *discprop (.=0) if respond==1
*gen otherdiscprop=othersum_numerator/totopinionateddisc
replace cirosum_numerator=. if (whennew==2 & wave==2) | (whennew==2 & wave==3) | (respond~=1) | (wave==1) | (wave>3) | (whennew==3)
replace lulasum_numerator=. if (whennew==2 & wave==2) | (whennew==2 & wave==3) | (respond~=1) | (wave==1) | (wave==4) | (whennew==3)
replace serrasum_numerator=. if (whennew==2 & wave==2) | (whennew==2 & wave==3) | (respond~=1) | (wave==1) | (wave>3) | (whennew==3)
replace agsum_numerator=. if (whennew==2 & wave==2) | (whennew==2 & wave==3) | (respond~=1) | (wave==1) | (wave>3) | (whennew==3)
*replace othersum_numerator=. if (whennew==2 & wave==2) | (whennew==2 & wave==3) | (respond~=1) | (wave==1) | (wave==4) | (whennew==3)
recode totopinionated 0=1 1/3=0 .=., gen(zerodiscstrong) 

*Weak disagreement 
recode v74c_disc1presvote v75c_disc2presvote v76c_disc3presvote (17/19=1) (1/16=0) (.=.) (else=0), gen(noop1 noop2 noop3)
egen disctotal=rsum(totopinionateddisc noop1 noop2 noop3)
egen nonesum_numerator=rsum(noop1 noop2 noop3)
replace disctotal=. if respond~=1 | (whennew==2 & wave==2) | (whennew==2 & wave==3) | (wave==1) | (wave==4) | (whennew==3)
gen cirodiscpropweak=cirosum_numerator / disctotal
gen luladiscpropweak= lulasum_numerator /disctotal
gen serradiscpropweak=serrasum_numerator /disctotal
gen agdiscpropweak= agsum_numerator/disctotal
*gen otherdiscpropweak=othersum_numerator/disctotal
gen nonediscpropweak=nonesum_numerator/disctotal
recode disctotal 0=1 1/3=0 .=., gen(zerodiscweak) 

*Neighborhood party strength score
factor partyintensePT v42c_pidthermpt_std if wave==1 & city==1, pcf factor(1)
predict PTfactor1
impute PTfactor1 partyintensePT v42c_pidthermpt_std if wave==1 & city==1, gen(PTfactorCX)

factor partyintensePMDB v42d_pidthermpmdb_std if wave==1 & city==1, pcf factor(1)
predict PMDBfactor1
impute PMDBfactor1 partyintensePMDB v42d_pidthermpmdb_std if wave==1 & city==1, gen(PMDBfactorCX)

factor partyintensePT v42c_pidthermpt_std if wave==1 & city==2, pcf factor(1)
predict PTfactor1j
impute PTfactor1j partyintensePT v42c_pidthermpt_std if wave==1 & city==2, gen(PTfactorJF)

factor partyintensePMDB v42d_pidthermpmdb_std if wave==1 & city==2, pcf factor(1)
predict PMDBfactor1j
impute PMDBfactor1j partyintensePMDB v42d_pidthermpmdb_std if wave==1 & city==2, gen(PMDBfactorJF)

factor partyintensePT v42c_pidthermpt_std partyintensePMDB v42d_pidthermpmdb_std if wave==1 & city==1, pcf factor(1)
predict PTPMDBfactor1
impute PTPMDBfactor1 partyintensePT v42c_pidthermpt_std partyintensePMDB v42d_pidthermpmdb_std if wave==1 & city==1, gen(PTPMDBfactorCX)

factor partyintensePT v42c_pidthermpt_std partyintensePMDB v42d_pidthermpmdb_std if wave==1 & city==2, pcf factor(1)
predict PTPMDBfactor2
impute PTPMDBfactor2 partyintensePT v42c_pidthermpt_std partyintensePMDB v42d_pidthermpmdb_std if wave==1 & city==2, gen(PTPMDBfactorJF)

getaggs PTfactorCX PMDBfactorCX PTfactorJF PMDBfactorJF	 PTPMDBfactorCX PTPMDBfactorJF
getaggs1 PTfactorCX PMDBfactorCX PTfactorJF PMDBfactorJF PTPMDBfactorCX PTPMDBfactorJF

replace PTfactorCXbairronomr =  PTfactorCXbairro if PTfactorCXbairronomr==.
replace PMDBfactorCXbairronomr =  PMDBfactorCXbairro if PMDBfactorCXbairronomr==.
replace PTfactorJFbairronomr =  PTfactorJFbairro if PTfactorJFbairronomr==.
replace PMDBfactorJFbairronomr =  PMDBfactorJFbairro if PMDBfactorJFbairronomr==.
replace PTPMDBfactorCXbairronomr =  PTPMDBfactorCXbairro if PTPMDBfactorCXbairronomr==.
replace PTPMDBfactorJFbairronomr =  PTPMDBfactorJFbairro if PTPMDBfactorJFbairronomr ==.

*read out for bairro convergence analysis
preserve
recode v22c_presvote 1=1 2=0 4=0 5=0 else=., gen(presvote_ciro_prop)
recode v22c_presvote 1=0 2=1 4=0 5=0 else=., gen(presvote_lula_prop)
recode v22c_presvote 1=0 2=0 4=1 5=0 else=., gen(presvote_serra_prop)
recode v22c_presvote 1=0 2=0 4=0 5=1 else=., gen(presvote_ag_prop)

keep presvote_ciro_prop presvote_lula_prop presvote_serra_prop presvote_ag_prop bairroibge wave id city PTfactorCXbairro PMDBfactorCXbairro PTfactorJFbairro PMDBfactorJFbairro  PTPMDBfactorCXbairro PTPMDBfactorJFbairro
drop if wave==.
reshape wide presvote_ciro presvote_lula presvote_serra presvote_ag PTfactorCXbairro PMDBfactorCXbairro PTfactorJFbairro PMDBfactorJFbairro PTPMDBfactorCXbairro PTPMDBfactorJFbairro, i(id) j(wave)
collapse (mean) presvote_ciro* presvote_lula* presvote_serra* presvote_ag* (p50) city PTfactorCXbairro1 PMDBfactorCXbairro1 PTfactorJFbairro1 PMDBfactorJFbairro1 PTPMDBfactorCXbairro1 PTPMDBfactorJFbairro1, by(bairroibge)
sort bairroibge 
save "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 5 Neighborhoods\Data\merge_neigh_wave", replace

restore

preserve

*Caxias: check on validity of our measures of neighborhood aggregate opinion and gen scatter by income
xtset id wave
gen PTfactorCXbairro_graph = l2.PTfactorCXbairro 
gen PMDBfactorCXbairro_graph =l2.PMDBfactorCXbairro  
gen PTPMDBfactorCXbairro_graph  =l2.PTPMDBfactorCXbairro  
gen presvote_ciro98bairro_graph=l2.presvote_ciro98bairro 
gen presvote_lula98bairro_graph=l2.presvote_lula98bairro 
gen presvote_fhc98bairro_graph=l2.presvote_fhc98bairro

keep if wave==3 & city==1
collapse (mean) presvote_cirobairro presvote_lulabairro presvote_agbairro presvote_serrabairro ciro_prop02 lula_prop02 garo_prop02 serra_prop02 presvote_fhc98 presvote_lula98 presvote_ciro98 fhc_prop98 lula_prop98 ciro_prop98 diff98 diff00 diff02 diff98gov (median) income lnincome ses PTfactorCXbairro_graph PMDBfactorCXbairro_graph PTPMDBfactorCXbairro_graph presvote_ciro98bairro_graph presvote_lula98bairro_graph presvote_fhc98bairro_graph dutra_prop_gov98 britto_prop_gov98 emilia_prop_gov98 pepe_prop00 sartori_prop00 sebe_prop00 bairro_pref bairro_pref_means, by(bairroibge)
drop if bairroibge==.
pwcorr presvote_cirobairro presvote_lulabairro presvote_agbairro presvote_serrabairro ciro_prop02 lula_prop02 garo_prop02 serra_prop02 income
	*1998
gen diff_ours_98=presvote_lula98bairro_graph/(presvote_lula98bairro_graph+presvote_fhc98bairro_graph)
gen diff_actual_98=diff98
gen diff_actual_98_gov=diff98gov 
pwcorr presvote_fhc98bairro_graph presvote_lula98bairro_graph fhc_prop98 lula_prop98 ciro_prop98 diff_ours_98 diff_actual_98 income, obs
	*2002
gen diff_ours_02=presvote_lulabairro/(presvote_lulabairro+presvote_serrabairro)
gen diff_actual_02=diff02
pwcorr diff_ours_02 diff_actual_02 PTfactorCXbairro_graph PMDBfactorCXbairro_graph PTPMDBfactorCXbairro_graph income , obs
	*2000
gen diff_actual_00=diff00
	
pwcorr diff_actual_98 diff_actual_98_gov diff_actual_00 income, obs

corr bairro_pref_mean income
 
label define csbairro 135 Centro 105 "Cidade Nova" 101 Cohab 120 "NS de Fátima" 145 Galópolis 127 "Jardelino Ramos" ///
119 "Jardim América" 108 Kayser 122 "NS de Lourdes" 118 Madureira 126 Petrópolis 116 Pioneiro 113 "Pio X" 123 Planalto 109 "Rio Branco" ///
121 "Sagrada Família" 107 "Santa Fé" 137 "São Cristóvão" 115 "São José" 102 "São Pelegrino" 103 "São Victor Cohab" 106 "Vila Ipé"
label values bairroibge csbairro 

reg bairro_pref_mean income
predict xb

scatter bairro_pref_mean xb income, msym(o i) mcolor(black black) mlab(bairroibge .) mlabcolor(black black) connect(. l) lcolor(black black) ///
sort note("r = -.66") legend(off) ///
 ytitle("Index of PT versus PMDB vote" "in recent executive elections" ) xtitle("Neighborhood Median Monthly Income (R$)") ///
 graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) xscale(range(550 1700)) ylab(none)

corr diff_ours_98 diff_actual_98 PTPMDBfactorCXbairro_graph 
keep bairroibge income
sort bairroibge
save "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 5 Neighborhoods\Data\income_CX.dta", replace
restore

preserve
xtset id wave
gen PTfactorJFbairro_graph  =l2.PTfactorJFbairro  
gen PMDBfactorJFbairro_graph	 = l2.PMDBfactorJFbairro	 
gen PTPMDBfactorJFbairro_graph = l2.PTPMDBfactorJFbairro 
gen presvote_ciro98bairro_graph=l2.presvote_ciro98bairro 
gen presvote_lula98bairro_graph=l2.presvote_lula98bairro 
gen presvote_fhc98bairro_graph=l2.presvote_fhc98bairro

*JF: check on validity of our measures of neighborhood aggregate opinion and gen scatter by income. Need to adjust once read in real elections data
keep if wave==3 & city==2
drop if bairroibge==.
collapse (mean) presvote_cirobairro presvote_lulabairro presvote_agbairro presvote_serrabairro (median) PTfactorJFbairro_graph PMDBfactorJFbairro_graph PTPMDBfactorJFbairro_graph presvote_fhc98bairro_graph presvote_lula98bairro_graph presvote_ciro98bairro_graph income ses bairro_pref bairro_pref_means, by(bairroibge)
drop if bairroibge==.
*pwcorr presvote_cirobairro presvote_lulabairro presvote_agbairro presvote_serrabairro ciro_prop02 lula_prop02 garo_prop02 serra_prop02 income
	*1998
gen diff_ours_98=presvote_lula98bairro_graph-presvote_fhc98bairro_graph
*gen diff_actual_98=lula_prop98-fhc_prop98
pwcorr presvote_fhc98bairro_graph presvote_lula98bairro_graph diff_ours_98 income, obs
	*2002
gen diff_ours_02=presvote_lulabairro-presvote_serrabairro
*gen diff_actual_02=lula_prop02-serra_prop02
pwcorr diff_ours_02 PTfactorJFbairro_graph PMDBfactorJFbairro_graph PTPMDBfactorJFbairro_graph income , obs

label define jfbairro 204 Industrial 206 Fábrica 216 "Poço Rico" 222 "Dom Bosco" 207 "Morro da Glória" 272 Bonfim 220 Passos 212 "Vitorino Braga" /// 
203 Bairu 278 "Santa Rita" 289 "Santo Antônio" 263 "NS de Lourdes" 215 "Costa Carvalho" 243 "Francisco Bernardino"  ///
282 Linhares 296 "Santa Luzia" 201 "Santa Terezinha" 267 "São Benedito" 279 Progresso 250 Benfica 219 "São Mateus" 214 Centro  
label values bairroibge jfbairro 

reg bairro_pref_mean income
predict xb

scatter bairro_pref_mean xb income, msym(o i) mcolor(black black) mlab(bairroibge .) mlabcolor(black black) connect(. l) lcolor(black black) ///
sort note("r = +.07") legend(off) ///
 ytitle("Index of PT versus non-left vote" "in recent executive elections" ) xtitle("Neighborhood Median Monthly Income (R$)") ///
 graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) xscale(range(550 2150)) ylab(none)

corr diff_ours_98 PTPMDBfactorJFbairro_graph
keep bairroibge income
sort bairroibge
save "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 5 Neighborhoods\Data\income_JF.dta", replace
restore

*Set up Vote Variables
recode v22c_presvote 1=1 2=2 3=16 4=4 5=5 6=16 7=16 8=16 9=16 10=10 11=16 12=12 13=16 14=16 16=16 17=17 18=17 19=17, gen(presvote)
label define presvotetemp 1 Ciro 2 Lula 3 Roseana 4 Serra 5 Garotinho  6 Itamar 7 "Jose Maria" 8 "Rui Costa Pimenta"  9 "Fernando Henrique Cardoso" 10 "Alckmin" 11 "Buarque" 12 "Helena" 13 "Eymael" 14 "Bivar" 16 Other 17 "No One" 18 DK 19 NR
label values presvote presvotetemp

recode v22c_presvote 1=4 2=1 3=16 4=19 5=2 6=16 7=16 8=16 9=16 10=10 11=16 12=12 13=16 14=16 16=16 17=16 18=16 19=16 .=16, gen(presvote_iv)
label define presvote_iv 4 Ciro 1 Lula 19 Serra 2 Garotinho 16 "Other/No One/DK/NR"
label values presvote_iv presvote_iv
replace presvote_iv=. if wave~=1


*Remember that any vars you want to use must be put in the next two commands
keep id wave city respond bairroibge presvote *discprop zerodiscstrong partyintensePT partyintensePMDB ///
 partyintensePSDB PTfactorCXbairronomr PMDBfactorCXbairronomr PTfactorJFbairronomr PMDBfactorJFbairronomr presvote_iv ///
presvote_cirobairronomr presvote_cirobairro presvote_lulabairronomr presvote_lulabairro presvote_agbairronomr presvote_agbairro evangelical_attend ///
presvote_serrabairronomr presvote_serrabairro ses lnincome educ lulacontact serracontact fhc_prop98 lula_prop98 ciro_prop98 ciro_prop02 lula_prop02 garo_prop02 serra_prop02 ///
s1_sex partyintense_baseline ses_iv presvote_fhc98bairro presvote_lula98bairro presvote_ciro98bairro presvote_fhc98bairronomr presvote_lula98bairronomr presvote_ciro98bairronomr ///
serradiscpropweak cirodiscpropweak luladiscpropweak agdiscpropweak nonediscpropweak zerodiscweak diff98 ///
v40k_issuesthermpriv v40l_issuesthermfreetrade v40m_issuesthermdeathpenalty v40n_issuesthermlandreform v63a_issuespriv v64_issuesfreetrade ///
v65_issuesalca v70a_issueslandreform cirocontact agcontact contactbaseline PTPMDBfactorCXbairronomr PTPMDBfactorJFbairronomr bairro_pref_means v22c_presvote disctotal converse1 v16e_conversefriends v16g_conversefamily v16b_conversenotamob   

drop if wave==.

reshape wide respond presvote *discprop zerodiscstrong partyintensePT partyintensePMDB ///
partyintensePSDB PTfactorCXbairronomr PMDBfactorCXbairronomr PTfactorJFbairronomr PMDBfactorJFbairronomr presvote_iv ///
presvote_cirobairronomr presvote_cirobairro presvote_lulabairronomr presvote_lulabairro presvote_agbairronomr presvote_agbairro ///
presvote_serrabairronomr presvote_serrabairro lnincome educ ses lulacontact serracontact s1_sex evangelical_attend partyintense_baseline ses_iv ///
presvote_fhc98bairro presvote_lula98bairro presvote_ciro98bairro presvote_fhc98bairronomr presvote_lula98bairronomr presvote_ciro98bairronomr ///
serradiscpropweak cirodiscpropweak luladiscpropweak agdiscpropweak nonediscpropweak zerodiscweak ///
v40k_issuesthermpriv v40l_issuesthermfreetrade v40m_issuesthermdeathpenalty v40n_issuesthermlandreform v63a_issuespriv v64_issuesfreetrade ///
v65_issuesalca v70a_issueslandreform cirocontact agcontact contactbaseline PTPMDBfactorCXbairronomr PTPMDBfactorJFbairronomr bairro_pref_means v22c_presvote disctotal converse1 v16e_conversefriends v16g_conversefamily v16b_conversenotamob  , i(id) j(wave) 

*********************
*does amount of disagreement in 'hood make you more likely to switch?
gen change23=0 if respond2==1 & respond3==1 & v22c_presvote2~=3 & v22c_presvote3~=3 & v22c_presvote2~=6 & v22c_presvote2~=9 & v22c_presvote2<17 & v22c_presvote3<17
replace change23=1 if v22c_presvote2~=v22c_presvote3 & change23==0
bysort change23: tab v22c_presvote2 v22c_presvote3, m
tab change23

gen neigh_disagree=1-ciro_prop98 if presvote2==1
replace neigh_disagree=1-lula_prop98 if presvote2==2
replace neigh_disagree=1-fhc_prop98 if presvote2==4
	*it's all Serra here
probit change23 neigh_disagree if city==1 & presvote2==1
margins, at(neigh_disagree=.905)
margins, at(neigh_disagree=.971)
probit change23 neigh_disagree if city==1 & presvote2==2
margins, at(neigh_disagree=.442)
margins, at(neigh_disagree=.743)
probit change23 neigh_disagree if city==1 & presvote2==4
margins, at(neigh_disagree=.35)
margins, at(neigh_disagree=.647)
*************************
xyz		
logit change23 i.presvote2##c.bairro_pref_means1 if city==1 & presvote2<5 , cluster(bairroibge)
margins, at(bairro_pref_means1==-1.6 presvote2==4)
margins, at(bairro_pref_means1==2.2 presvote2==4)
margins, at(bairro_pref_means1==-1.6 presvote2==2)
margins, at(bairro_pref_means1==2.2 presvote2==2)
margins, at(bairro_pref_means1==-1.6 presvote2==1)
margins, at(bairro_pref_means1==2.2 presvote2==1)

tab presvote2 presvote3 if bairro_pref_means1< -.9237242 & city==1 & presvote2<6 & presvote3<6, row
tab presvote2 presvote3 if bairro_pref_means1>.8767175 & city==1 & presvote2<6 & presvote3<6, row


*ISSUES
mvdecode v63a_issuespriv2 v64_issuesfreetrade3 v65_issuesalca3 v70a_issueslandreform2, mv(8)
mvdecode v63a_issuespriv2 v64_issuesfreetrade3 v65_issuesalca3 v70a_issueslandreform2, mv(9)
mvdecode v40k_issuesthermpriv2 v40l_issuesthermfreetrade2 v40m_issuesthermdeathpenalty2 v40n_issuesthermlandreform2, mv(18)
mvdecode v40k_issuesthermpriv2 v40l_issuesthermfreetrade2 v40m_issuesthermdeathpenalty2 v40n_issuesthermlandreform2, mv(19)

egen rmean=rmean(v40k_issuesthermpriv2 v40l_issuesthermfreetrade2 v40m_issuesthermdeathpenalty2 v40n_issuesthermlandreform2)
egen rsd=rsd(v40k_issuesthermpriv2 v40l_issuesthermfreetrade2 v40m_issuesthermdeathpenalty2 v40n_issuesthermlandreform2)
recode rsd 0=.1
gen ftprivz= (v40k_issuesthermpriv2-rmean)/rsd
gen ftftz= (v40l_issuesthermfreetrade2-rmean)/rsd
gen ftlrz= (v40n_issuesthermlandreform2-rmean)/rsd

factor ftprivz v63a_issuespriv2  ftftz v64_issuesfreetrade3 v65_issuesalca3 ftlrz v70a_issueslandreform2 , pcf factors(1)
predict tempeconissues1
impute tempeconissues1  ftprivz v63a_issuespriv2  ftftz v64_issuesfreetrade3 v65_issuesalca3 ftlrz v70a_issueslandreform2 if respond3==1, gen(econissues)
replace econissues=-econissues


*really sensitive to other, so I neutralized it

local PTCX05=-1.479
local PTCX95=1.8925

local PTJF05=-1.65
local PTJF95=2.21


*DISCUSSANT CHOICE REGS
eststo clear
*using election returns 
*Figure 5.6 and Table A.12 
	*CX
eststo: fmlogit serradiscprop2 cirodiscprop2 luladiscprop2 agdiscprop2 if zerodiscstrong2==0 & city==1, ///
etavar(bairro_pref_means1 partyintensePT1 partyintensePMDB1 partyintense_baseline1 ib19.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) cluster(bairroibge)
*bootstrap, reps(100) cluster(bairroibge) seed(3838)

margins , at(bairro_pref_means1=`PTCX05'  (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscprop2)) predict(outcome(luladiscprop2 )) predict(outcome(agdiscprop2)) predict(outcome(cirodiscprop2))  
margins , at(bairro_pref_means1=`PTCX95' (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscprop2)) predict(outcome(luladiscprop2 )) predict(outcome(agdiscprop2)) predict(outcome(cirodiscprop2))  
	
	*JF
eststo: fmlogit serradiscprop2 cirodiscprop2 luladiscprop2 agdiscprop2 if zerodiscstrong2==0 & city==2, ///
etavar(bairro_pref_means1 partyintensePT1 partyintensePMDB1 partyintense_baseline1 ib19.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) cluster(bairroibge)

margins , at(bairro_pref_means1=`PTJF05'  (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscprop2)) predict(outcome(luladiscprop2 )) predict(outcome(agdiscprop2)) predict(outcome(cirodiscprop2))  
margins , at(bairro_pref_means1=`PTJF95' (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscprop2)) predict(outcome(luladiscprop2 )) predict(outcome(agdiscprop2)) predict(outcome(cirodiscprop2))  

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\\Chapter 5 Neighborhoods\Code\Output\"
esttab est1 est2 using Brazil2002_disc_choice.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Table 5.x. The Correlates of Discussant Choice in Two Brazilian Cities, 2002) ///
modelwidth(6) compress ///
addnote("Entries are quasi maximum likelihood fractional multinomial logit coefficients with bootstrapped standard errors (corrected for clustering by neighborhood) in parentheses.")

*MLOGIT VOTE CHOICE REGS
	*CX
eststo clear
quietly mlogit presvote3 bairro_pref_means1 partyintensePT1 partyintensePMDB1 partyintense_baseline1 ses_iv1 s1_sex1 evangelical_attend1 luladiscprop2 cirodiscprop2 agdiscprop2 zerodiscstrong2 if city==1 & presvote3<16, cluster(bairroibge) b(4) 
gen esample=1 if e(sample)==1
*Table 5.5 and Table A.13, A.14

eststo clear
eststo: mlogit presvote3 bairro_pref_means1 ib19.presvote_iv1 if city==1 & presvote3<16 & esample==1,  b(4) cluster(bairroibge)
eststo: mlogit presvote3 bairro_pref_means1 ib19.presvote_iv1 ses_iv1 econissues partyintensePT1 partyintensePMDB1 partyintense_baseline1 s1_sex1 if city==1 & presvote3<16 & esample==1,  b(4) cluster(bairroibge)
eststo: mlogit presvote3 bairro_pref_means1 ib19.presvote_iv1 ses_iv1 econissues partyintensePT1 partyintensePMDB1 partyintense_baseline1 s1_sex1 evangelical_attend1 lulacontact3 agcontact3 cirocontact3 contactbaseline3 if city==1 & presvote3<16 & esample==1,  b(4)
eststo: mlogit presvote3 bairro_pref_means1 ib19.presvote_iv1 ses_iv1 econissues partyintensePT1 partyintensePMDB1 partyintense_baseline1 s1_sex1 evangelical_attend1 lulacontact3 agcontact3 cirocontact3 contactbaseline3 luladiscprop2 agdiscprop2 cirodiscprop2 zerodiscstrong2 if city==1 & presvote3<16 & esample==1,  b(4)

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\\Chapter 5 Neighborhoods\Code\Output\"
esttab est1 est2 est3 est4 using Brazil2002_MNLs_neigh_presvoteCX.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Correlates of Presidential Vote Choice in Two Brazilian Cities, 2002) ///
modelwidth(6) compress ///
addnote("Entries are maximum likelihood multinomial logit coefficients with bootstrapped standard errors (corrected for clustering by neighborhood) in parentheses.")

	*JF
drop esample
eststo clear
quietly mlogit presvote3 bairro_pref_means1 partyintensePT1 partyintensePMDB1 partyintense_baseline1 ses_iv1 s1_sex1 evangelical_attend1 luladiscprop2 cirodiscprop2 agdiscprop2 zerodiscstrong2 if city==2 & presvote3<16, cluster(bairroibge) b(4) 
gen esample=1 if e(sample)==1

eststo clear
eststo: mlogit presvote3 bairro_pref_means1 ib19.presvote_iv1 if city==2 & presvote3<16 & esample==1,  b(4) cluster(bairroibge)
eststo: mlogit presvote3 bairro_pref_means1 ib19.presvote_iv1 ses_iv1 econissues partyintensePT1 partyintensePMDB1 partyintense_baseline1 s1_sex1 if city==2 & presvote3<16 & esample==1,  b(4) cluster(bairroibge)
eststo: mlogit presvote3 bairro_pref_means1 ib19.presvote_iv1 ses_iv1 econissues partyintensePT1 partyintensePMDB1 partyintense_baseline1 s1_sex1 evangelical_attend1 lulacontact3 agcontact3 cirocontact3 contactbaseline3 if city==2 & presvote3<16 & esample==1,  b(4)
eststo: mlogit presvote3 bairro_pref_means1 ib19.presvote_iv1 ses_iv1 econissues partyintensePT1 partyintensePMDB1 partyintense_baseline1 s1_sex1 evangelical_attend1 lulacontact3 agcontact3 cirocontact3 contactbaseline3 luladiscprop2 agdiscprop2 cirodiscprop2 zerodiscstrong2 if city==2 & presvote3<16 & esample==1,  b(4)

eststo: mlogit presvote3 ib19.presvote_iv1 ses_iv1 econissues partyintensePT1 partyintensePMDB1 partyintense_baseline1 s1_sex1 evangelical_attend1 lulacontact3 agcontact3 cirocontact3 contactbaseline3 converse11 if city==2 & presvote3<16 & esample==1,  b(4)
margins, at(converse11=1) atmeans
margins, at(converse11=4) atmeans
xyz
cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\\Chapter 5 Neighborhoods\Code\Output\"
esttab est1 est2 est3 est4 using Brazil2002_MNLs_neigh_presvoteJF.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Table 6.5A. Correlates of Presidential Vote Choice in Two Brazilian Cities, 2002) ///
modelwidth(6) compress ///
addnote("Entries are maximum likelihood multinomial logit coefficients with bootstrapped standard errors (corrected for clustering by neighborhood) in parentheses.")

xyz



*APPENDIX: weak disagreement
eststo clear
eststo: eststo: bootstrap, reps(100) cluster(bairroibge) seed(3838): fmlogit serradiscpropweak2 cirodiscpropweak2 luladiscpropweak2 agdiscpropweak2 if zerodiscweak2==0 & city==1, ///
etavar(PTfactorCXbairronomr1 PMDBfactorCXbairronomr1 partyintensePT1 partyintensePMDB1 partyintense_baseline1 ib19.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) 

margins , at(PTfactorCXbairronomr1 =`PTCX95' PMDBfactorCXbairronomr1 =`PMDBCX05' (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscpropweak2)) predict(outcome(luladiscpropweak2 )) predict(outcome(agdiscpropweak2)) predict(outcome(cirodiscpropweak2))  
margins , at(PTfactorCXbairronomr1 =`PTCX05' PMDBfactorCXbairronomr1 =`PMDBCX95' (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscpropweak2)) predict(outcome(luladiscpropweak2 )) predict(outcome(agdiscpropweak2)) predict(outcome(cirodiscpropweak2))  

eststo: eststo: bootstrap, reps(100) cluster(bairroibge) seed(3838): fmlogit serradiscpropweak2 cirodiscpropweak2 luladiscpropweak2 agdiscpropweak2 if zerodiscweak2==0 & city==2, ///
etavar(PTfactorJFbairronomr1 PMDBfactorJFbairronomr1 partyintensePT1 partyintensePMDB1 partyintense_baseline1 ib19.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1)

margins , at(PTfactorJFbairronomr1 =`PTJF95' PMDBfactorJFbairronomr1 =`PMDBJF05' (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscpropweak2)) predict(outcome(luladiscpropweak2 )) predict(outcome(agdiscpropweak2)) predict(outcome(cirodiscpropweak2))  
margins , at(PTfactorJFbairronomr1 =`PTJF05' PMDBfactorJFbairronomr1 =`PMDBJF95' (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscpropweak2)) predict(outcome(luladiscpropweak2 )) predict(outcome(agdiscpropweak2)) predict(outcome(cirodiscpropweak2))  

eststo: eststo: bootstrap, reps(100) cluster(bairroibge) seed(3838): fmlogit serradiscpropweak2 cirodiscpropweak2 luladiscpropweak2 agdiscpropweak2 if zerodiscweak2==0 & city==1, ///
etavar(bairro_pref_means1 partyintensePT1 partyintensePMDB1 partyintense_baseline1 ib19.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1)

margins , at(bairro_pref_means1=-1.4524  (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscpropweak2)) predict(outcome(luladiscpropweak2 )) predict(outcome(agdiscpropweak2)) predict(outcome(cirodiscpropweak2))  
margins , at(bairro_pref_means1=1.8598 (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscpropweak2)) predict(outcome(luladiscpropweak2 )) predict(outcome(agdiscpropweak2)) predict(outcome(cirodiscpropweak2))  

/*
	*JF, when ready
eststo: fmlogit serradiscpropweak2 cirodiscpropweak2 luladiscpropweak2 agdiscpropweak2 if zerodiscweak2==0 & city==2, ///
etavar(diff98vote partyintensePT1 partyintensePMDB1 partyintense_baseline1 ib19.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) cluster(bairroibge)

margins , at(diff98vote=`PTXCvote95'  (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscpropweak2)) predict(outcome(luladiscpropweak2 )) predict(outcome(agdiscpropweak2)) predict(outcome(cirodiscpropweak2))  
margins , at(diff98vote=`PTXCvote05' (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscpropweak2)) predict(outcome(luladiscpropweak2 )) predict(outcome(agdiscpropweak2)) predict(outcome(cirodiscpropweak2))  
*/

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\\Chapter 5 Neighborhoods\code\output\"
esttab est1 est2 using Brazil2002_disc_choice_weak.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Table 6.5A. The Correlates of Discussant Choice in Two Brazilian Cities, 2002: weak definition) ///
modelwidth(6) compress ///
addnote("Entries are quasi maximum likelihood fractional multinomial logit coefficients with robust standard errors (corrected for clustering by neighborhood) in parentheses.")

	*Using first wave partisanship
eststo clear
eststo: eststo: bootstrap, reps(100) cluster(bairroibge) seed(3838): fmlogit serradiscprop2 cirodiscprop2 luladiscprop2 agdiscprop2 if zerodiscstrong2==0 & city==1, ///
etavar(PTfactorCXbairronomr1 PMDBfactorCXbairronomr1 partyintensePT1 partyintensePMDB1 partyintense_baseline1 ib19.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1)

margins , at(PTfactorCXbairronomr1 =`PTCX95' PMDBfactorCXbairronomr1 =`PMDBCX05' (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscprop2)) predict(outcome(luladiscprop2 )) predict(outcome(agdiscprop2)) predict(outcome(cirodiscprop2))  
margins , at(PTfactorCXbairronomr1 =`PTCX05' PMDBfactorCXbairronomr1 =`PMDBCX95' (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscprop2)) predict(outcome(luladiscprop2 )) predict(outcome(agdiscprop2)) predict(outcome(cirodiscprop2))  

eststo: eststo: bootstrap, reps(100) cluster(bairroibge) seed(3838): fmlogit serradiscprop2 cirodiscprop2 luladiscprop2 agdiscprop2 if zerodiscstrong2==0 & city==2, ///
etavar(PTfactorJFbairronomr1 PMDBfactorJFbairronomr1 partyintensePT1 partyintensePMDB1 partyintense_baseline1 ib19.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) 

margins , at(PTfactorJFbairronomr1 =`PTJF95' PMDBfactorJFbairronomr1 =`PMDBJF05' (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscprop2)) predict(outcome(luladiscprop2 )) predict(outcome(agdiscprop2)) predict(outcome(cirodiscprop2))  
margins , at(PTfactorJFbairronomr1 =`PTJF05' PMDBfactorJFbairronomr1 =`PMDBJF95' (mean) partyintensePT1 partyintensePMDB1 1.presvote_iv1 2.presvote_iv1 4.presvote_iv1 16.presvote_iv1 ses_iv1 s1_sex1 evangelical_attend1) predict(outcome(serradiscprop2)) predict(outcome(luladiscprop2 )) predict(outcome(agdiscprop2)) predict(outcome(cirodiscprop2))  

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\\Chapter 5 Neighborhoods\Code\Output\"
esttab est1 est2 using Brazil2002_disc_choice_pid.rtf, label b(3) replace se star(* 0.05 ) nogap onecell ///
title(Table 6.5. The Correlates of Discussant Choice in Two Brazilian Cities, 2002) ///
modelwidth(6) compress ///
addnote("Entries are quasi maximum likelihood fractional multinomial logit coefficients with robust standard errors (corrected for clustering by neighborhood) in parentheses.")

